package me.zhengjie.modules.lock.service.impl;


import com.zelk.lock.api.clent.feign.LockDeviceCmdApi;
import com.zelk.lock.api.dto.LockDeviceCmdListDTO;
import com.zelk.lock.api.vo.LockDeviceCmdPageVO;
import com.zelk.smartlock.common.response.ResponsePage;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.lock.mapping.MappingUtil;
import me.zhengjie.modules.lock.service.LockDeviceCmdService;
import me.zhengjie.modules.lock.service.dto.LockCmdListDTO;
import me.zhengjie.modules.lock.service.vo.LockDeviceCmdListReqVO;
import me.zhengjie.modules.lock.util.RequestCall;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class LockDeviceCmdServiceImpl implements LockDeviceCmdService {
    @Resource
    private LockDeviceCmdApi lockDeviceCmdApi;

    @Autowired
    private MappingUtil mappingUtil;

    @Override
    public Map<String, Object> getLockCmdList(LockDeviceCmdListReqVO lockDeviceCmdListReqVO, Pageable pageable) {
        LockDeviceCmdPageVO lockDevicePwdPageVO = mappingUtil.toLockDeviceCmdPageVO(lockDeviceCmdListReqVO);
        lockDevicePwdPageVO.setPageIdx((long) pageable.getPageNumber());
        lockDevicePwdPageVO.setPageSize(pageable.getPageSize() <= 0 ? 50L : (long) pageable.getPageSize());
        ResponsePage<List<LockDeviceCmdListDTO>> listResponsePage = RequestCall.executePage(lockDeviceCmdApi::selectLockDeviceCmdList, lockDevicePwdPageVO, null);
        List<LockDeviceCmdListDTO> lockDeviceCmdListDTOS = Optional.ofNullable(listResponsePage.getData()).orElse(new ArrayList<>());
        List<LockCmdListDTO> deviceInfoListDTOS = lockDeviceCmdListDTOS.stream().map(v -> {
            LockCmdListDTO lockCmdListDTO = mappingUtil.toLockCmdListDTO(v);
            //TODO 还要填充商户名称
            return lockCmdListDTO;
        }).collect(Collectors.toList());
        return PageUtil.toPage(deviceInfoListDTOS, Objects.nonNull(listResponsePage.getTotal()) ? listResponsePage.getTotal() : 0);
    }

    @Override
    public Map<String, String> getLockCmdPerformInfo(Long cmdId) {
        return lockDeviceCmdApi.selectLockCmdPerformInfo(cmdId);
    }
}
